package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResSysLogin;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginDataBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginUserprofileBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginValidityBean;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MobileFastLoginModel;
import com.rjwh.dingdong.client.mvp.presenter.IMobileFastLoginPresenter;
import com.rjwh.dingdong.client.mvp.view.MobileFastLoginView;
import com.rjwh.dingdong.client.network.ApiRequest;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName MobileFastLoginPresenter
 * @Description
 * @date 2016-10-13
 * @history 2016-10-13 author: description:
 */

public class MobileFastLoginPresenter extends BasePresenter<MobileFastLoginView> implements IMobileFastLoginPresenter {

    private MobileFastLoginModel model;

    public MobileFastLoginPresenter(Context context, MobileFastLoginView mMvpView) {
        super(context, mMvpView);
        model = new MobileFastLoginModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.SYSSMSCODELOGIN:
                    ResSysLogin resSysLogin = (ResSysLogin) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resSysLogin != null) {
                        SysLoginDataBean data = resSysLogin.getData();
                        if (data != null) {
                            SysLoginUserprofileBean userprofile = data.getUserprofile();
                            SysLoginValidityBean validity = data.getValidity();
                            List<SysLoginBabylistBean> babylist = data.getBabylist();

                            if (userprofile != null) {
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERID, userprofile.getUserid());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERPHONE, userprofile.getPhone());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERNAME, userprofile.getNickname());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERIMAGE, userprofile.getAvatar());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCOMPLETE, userprofile.getComplete());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERLEVEL, userprofile.getUserlevel());
                            }

                            if (validity != null) {
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_TOKEN, validity.getToken());
                                MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USER_HASPWD, validity.getHaspwd());
                            }

                            if (babylist != null && !babylist.isEmpty()) {

                                for (SysLoginBabylistBean sysLoginBabylistBean : babylist) {
                                    String chief = sysLoginBabylistBean.getChief();
                                    if (chief.equals("1")) {//取当前绑定的宝宝
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYID, sysLoginBabylistBean.getBabyid());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYNAME, sysLoginBabylistBean.getBabyname());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYSEX, sysLoginBabylistBean.getSex());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYBIRTHDAY, sysLoginBabylistBean.getBirthday());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_USERCHENGWEI, sysLoginBabylistBean.getTitle());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAVATAR, sysLoginBabylistBean.getBabyavatar());
                                        MyApplication.spUtil.setStrPreference(getContext(), LocalConstant.SP_BABYAGE, sysLoginBabylistBean.getBabyage());
                                    }
                                }
                            }

                            MyApplication.spUtil.setBoolPreference(getContext(), LocalConstant.SP_IS_LOGINED, true);
                            try {
                                ApplicationInfo appInfo = getContext().getPackageManager()
                                        .getApplicationInfo(getContext().getPackageName(),
                                                PackageManager.GET_META_DATA);
                                String msg = appInfo.metaData.getString("PUSH_APPID");
                                LogUtil.e("MobileFastLoginPresenter", "PUSH_APPID:" + msg);
                                if (!AsStrUtils.isEmpty(msg)) {
                                    LogUtil.d("MobileFastLoginPresenter", "绑定推送");
                                    ApiRequest.pushBindingForAppId(msg, MyApplication.spUtil.getStrPreferenceByParamName(getContext(), LocalConstant.SP_CLIENTID), null);
                                }
                            } catch (PackageManager.NameNotFoundException e) {
                                e.printStackTrace();
                            }
                            getMvpView().LoadData();

                        }
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;
                case NetConstant.TYPE_SMS_CODE:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().setTimerData();
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 手机号快捷登录
     *
     * @param account 手机号
     * @param smscode 验证码
     */
    @Override
    public void doSysSmsCodelogin(String account, String smscode) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doSysSmsCodelogin(account, smscode);
        }
    }

    /**
     * 获取验证码
     *
     * @param inputPhone 手机号
     */
    @Override
    public void doGetCode(String inputPhone) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doGetCode(inputPhone);
        }
    }
}
